package yang;


import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class TestEsClient {

    public static void main(String[] args) {
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials("fintech@small_online", "fintech");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("10.50.192.179", 8900), creds);

        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);
        HttpGet httpget = new HttpGet("http://10.50.192.179:8900/_cluster/health?pretty");

        try {
            CloseableHttpResponse response = httpclient.execute(httpget, context);
            HttpEntity entity = response.getEntity();
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
                System.out.println(EntityUtils.toString(entity));
                EntityUtils.consume(entity);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        HttpPost postRequest = new HttpPost(
                "http://10.50.192.179:8900/fintech.creditiq.news1/_search?pretty");
/*
      StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
12         entity.setContentEncoding("UTF-8");
 */

        Map<String, Object> map = new HashMap();
        Map<String, Object> query = new HashMap();
        query.put("match_all", new HashMap<String, Object>());
        map.put("query", query);
        String query_str = JSON.toJSONString(map);
        try {
            StringEntity input = new StringEntity(query_str);
            input.setContentType("application/json");
            postRequest.setEntity(input);
            CloseableHttpResponse response = httpclient.execute(postRequest, context);
            HttpEntity entity = response.getEntity();
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
                System.out.println(EntityUtils.toString(entity));
                EntityUtils.consume(entity);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
